//!Includes

#include "RobotConection.h"


//!Global Functions


namespace algorithms
{

/**
 * class RobotConection
 *
 *
 * Autor:David Bevilaqua
 *
 *
 * DESCRIPTION:
 *
 */

//!Constructors/Destructors

/**
 * Empty Constructor
 */
RobotConection::RobotConection ( )
{

}

/**
 * Empty Destructor
 */
RobotConection::~RobotConection ( )
{
printf("Conection caiu\n");
	m_pClient->disconnect();
	Aria::shutdown();
}

//!Public Accessor Methods
//

void RobotConection::SetArClient(ArClientBase *client)
{
m_pClient=client;
}

ArClientBase* RobotConection::GetArClient()
{
return m_pClient;
}
//!Public Methods
//
void RobotConection::jpegHandler(ArNetPacket *packet)
{
  unsigned int width;
  unsigned int height;
  static unsigned char jpeg[50000];
  int jpegSize;
  FILE *file;

  width = packet->bufToUByte2();
  height = packet->bufToUByte2();
  jpegSize = packet->getDataLength() - packet->getDataReadLength();
  if(jpegSize > 50000)
  {
    ArLog::log(ArLog::Normal, "Cannot save image, it's too big. (image is %d bytes, my buffer is 50000 bytes)", jpegSize);
    return;
  }
  packet->bufToData((char *)jpeg, jpegSize);
  char filename[128];
  char tmpFilename[128];
  sprintf(tmpFilename, "tmp.jpg");
  if ((file = ArUtil::fopen(tmpFilename, "wb")) != NULL)
  {
    fwrite(jpeg, jpegSize, 1, file);
    fclose(file);
    strcpy(filename, "temp.jpg");

    if (rename(tmpFilename, filename) == 0)
    {

    }
 //     ArLog::log(ArLog::Normal, "Wrote a %dx%d image, %d bytes, named %s.", width, height, jpegSize, filename);
  //  else
  //    ArLog::log(ArLog::Normal, "Wrote a %dx%d image, %d bytes, named %s (could not rename to real name).", width, height, jpegSize, tmpFilename);

  }
  else
    ArLog::log(ArLog::Normal, "Could not write temp file %s", tmpFilename);


}

void RobotConection::Escape( )
{
	printf("esc pressed, shutting down aria\n");
	Aria::shutdown();
}

void RobotConection::ConectWithRobot(int argc,char **argv)
{
	Aria::init();
	m_pParser=new ArArgumentParser(&argc, argv);
	m_pClientConnector=new ArClientSimpleConnector(m_pParser);
	m_pParser->loadDefaultArguments();

	/* Check for -help, and unhandled arguments: */
	if (!Aria::parseArgs() || !m_pParser->checkHelpAndWarnUnparsed())
	{
		Aria::logOptions();
		exit(0);
	}


	/* Connect our m_pClient object to the remote server: */
	if (!m_pClientConnector->connectClient(m_pClient))
	{
		if (m_pClient->wasRejected())
			printf("Server '%s' rejected connection, exiting\n", m_pClient->getHost());
		else
			printf("Could not connect to server '%s', exiting\n", m_pClient->getHost());
		exit(1);
	}

	printf("Connected to server.\n");

	m_pClient->setRobotName(m_pClient->getHost()); // include server name in log messages
	/* Create a key handler and also tell Aria about it */
	Aria::setKeyHandler(&keyHandler);
	escapeCB=ArGlobalFunctor (&Escape);
	keyHandler.addKeyHandler(ArKeyHandler::ESCAPE, &escapeCB);
	jpegHandlerCB=ArGlobalFunctor1<ArNetPacket *>(&jpegHandler);
	m_pClient->addHandler("sendVideo", &jpegHandlerCB);



	m_pClient->runAsync();










}

//!Protected Methods
//


//!Private Methods
//

/**
 * Starts all the attributes of
 * the RobotConection
 */
}



